/**
 * Created by Lethe on 2017/8/28.
 */
$(function () {
    var visibleNum = $("#visibleNum").val();
    var itemNum = $(".col-xs-3").length;
    var operID = undefined;
    window.Pics = {
        changeVisible: function (obj) {
            var id = $(obj).parent().parent("li").find(":hidden").val();
            SysTool.Ajax.ajaxJsonCall(
                $(obj).is(":checked") ? "banners/showPic" : "banners/hidePic",
                {id: id},
                function (response) {
                    if (response.type === 'hide') {
                        visibleNum--;
                        var html = $(obj).parent().parent("li").html();
                        if (visibleNum - 1 >= 0) {
                            // $(obj).parent().parent("li").remove();
                            // $($(".col-xs-3")[visibleNum - 1]).after('<li class="col-xs-3 ID' + id + '" style="margin: 0; border: thin solid #ccc; padding:0px; margin-top:5px">' + html + '</li>')
                        }
                        $(".ID" + id).find(":checkbox").removeAttr("checked");
                        $(".ID" + id).find(".pull-left").after('<label class="pull-right inline delete" style="margin: 3px">' +
                            '<button class="btn btn-xs btn-danger" onclick="Pics.delete(this)">删除</button>' +
                            '</label>' +
                            '<label class="pull-right inline edit" style="margin: 3px">' +
                            '<button class="btn btn-xs btn-purple" onclick="Pics.update(this)">编辑</button>' +
                            '</label>')
                    } else {
                        visibleNum++;
                        var html = $(obj).parent().parent("li").html();
                        if (visibleNum < itemNum) {
                            // $(obj).parent().parent("li").remove();
                            // $($(".col-xs-3")[visibleNum - 1]).before('<li class="col-xs-3 ID' + id + '" style="margin: 0; border: thin solid #ccc; padding:0px; margin-top:5px">' + html + '</li>')
                        }
                        $(".ID" + id).find(":checkbox").attr("checked", 'checked');
                        $(".ID" + id).find(".delete").remove();
                        $(".ID" + id).find(".edit").remove()
                    }
                }
            )
        },
        delete: function (obj) {
            operID = $(obj).parents("li").find(":hidden").val();
            var url = $(obj).parent().parent("li").find("img").attr("src");
            var target = $(obj).parent().parent("li").find("img").attr("alt");
            $("#delete_pic_modal").find("img").attr("src", url);
            $("#delete_pic_modal").find(".label-info").html(target)
            $("#delete_pic_modal").modal('show')
        },
        deleteAjx: function () {
            SysTool.Ajax.ajaxJsonCall("banners/deletePic",
                {id: operID},
                function (response) {
                    if (response.type == 'delete') {
                        $(".ID" + operID).remove()
                        $("#delete_pic_modal").modal('hide')
                    }
                })
        },
        update: function (obj) {
            operID = $(obj).parents("li").find(":hidden").val();
            var url = $(obj).parent().parent("li").find("img").attr("src");
            var target = $(obj).parent().parent("li").find("img").attr("alt");
            $("#update_pic_modal").find("img").attr("src", url);
            $("#update_pic_modal").find(":text").val(target)
            $("#update_pic_modal").modal('show')
        },
        updateAjx: function () {
            var target = $("#update_pic_modal").find(":text").val();
            SysTool.Ajax.ajaxJsonCall("banners/updateTarget",
                {id: operID, target: target},
                function (response) {
                    if (response.type == 'target') {
//                            $(".ID"+deleteID).remove()
                        $(".ID" + operID).find("img").attr("alt", target)
                        $(".ID" + operID).find(".label-info").html(target)
                        $("#update_pic_modal").modal('hide')
                    }
                })
        },
        uploadAjx: function () {
            if ($("#upload_pic_form").find(":file").val() === '') {
                BootBox.warning("请上传图片");
                return false;
            }
            SysTool.Ajax.ajaxSubmitForm($("#upload_pic_form"), function (data) {
                window.location.reload();
            })
        },
        bindDraggable: function () {
            $(".ace-thumbnails").sortable({
                revert: true,
                items: "> .sort-v",
                update: function (event, ui) {
                    var lis = $(event.target).find("li")
                    window.testUI = ui;
                    window.testEvent = event;
                    var itemClass = $(ui.item).attr("class");
                    var id = $(ui.item).find(":hidden").val();
                    for (var i = 1; i <= lis.length; i++) {
                        if ($(lis[i - 1]).attr("class") === itemClass) {
                            console.log(id + "  " + i);
                            SysTool.Ajax.ajaxJsonCall("banners/updateRank",
                                {id: id, rank: i},
                                function (response) {
                                })
                        }
                    }
                }
            })
        }

    };

    // Pics.bindDraggable();

    $("#upload-btn").click(function () {
        $("#upload_pic_modal").modal('show');
    });

    $('#id-input-file-3').ace_file_input({
        style: 'well',
        btn_choose: '选择图片',
        btn_change: null,
        no_icon: 'icon-picture',
        droppable: true,
        thumbnail: 'fit',
        preview_error: function (filename, error_code) {
        }
    }).on('change', function () {
    });
});
